package com.lsy.android.pingpang.view;

import java.util.ArrayList;
import java.util.Set;

import com.lsy.android.pingpang.R;

import android.app.Activity;
import android.app.Dialog;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class BluetoothView extends FrameLayout {

	private BluetoothAdapter mBtAdapter;
	private ArrayList<String> mDevicesNameArray = new ArrayList<String>();
	private ArrayList<String> mDevicesAddressArray = new ArrayList<String>();
	private BluetoothViewAdapter mAdapter;

	public BluetoothView(final Activity activity, Dialog dialog,
			final DataCallback callback) {
		super(activity);
		View view = LayoutInflater.from(activity).inflate(
				R.layout.bluetooth_dialog, null);
		addView(view);

		ListView pairedListView = (ListView) view
				.findViewById(R.id.paired_devices);
		mAdapter = new BluetoothViewAdapter(activity, mDevicesNameArray);
		pairedListView.setAdapter(mAdapter);
		pairedListView.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				mBtAdapter.cancelDiscovery();
				String address = mDevicesAddressArray.get(position);
				if (!TextUtils.isEmpty(address)) {
					callback.onReturnData(address);
				}
			}
		});

		Button backButton = (Button) view.findViewById(R.id.button_back);
		backButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				activity.finish();
			}
		});

		IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
		activity.registerReceiver(mReceiver, filter);
		filter = new IntentFilter(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
		activity.registerReceiver(mReceiver, filter);

		mBtAdapter = BluetoothAdapter.getDefaultAdapter();

		Set<BluetoothDevice> pairedDevices = mBtAdapter.getBondedDevices();
		if (pairedDevices.size() > 0) {
			for (BluetoothDevice device : pairedDevices) {
				mDevicesNameArray.add(device.getName());
				mDevicesAddressArray.add(device.getAddress());
				mAdapter.notifyDataSetChanged();
			}
		}
		doDiscovery();
	}

	@Override
	protected void onDetachedFromWindow() {
		getContext().unregisterReceiver(mReceiver);
		super.onDetachedFromWindow();
	}

	private void doDiscovery() {
		if (mBtAdapter.isDiscovering()) {
			mBtAdapter.cancelDiscovery();
		}
		mBtAdapter.startDiscovery();
	}

	private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();
			if (BluetoothDevice.ACTION_FOUND.equals(action)) {
				BluetoothDevice device = intent
						.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
				if (device.getBondState() != BluetoothDevice.BOND_BONDED) {
					mDevicesNameArray.add(device.getName());
					mDevicesAddressArray.add(device.getAddress());
					mAdapter.notifyDataSetChanged();
				}
			} else if (BluetoothAdapter.ACTION_DISCOVERY_FINISHED
					.equals(action)) {
				if (mDevicesNameArray.size() == 0) {
					Toast.makeText(context, "没有找到新的手机！", Toast.LENGTH_SHORT)
							.show();
				}
			}
		}
	};

	public interface DataCallback {
		public void onReturnData(String adress);
	}

}
